<?php
namespace App\Admin\Controllers;
use App\Admin\Models\BShopRoles;
use App\Admin\Models\BShopRule;
use Common\ControllerAdBase;
use Common\Models\BShopGoods;
use Common\Models\BShopUsers;
use Common\Models\BshopUsersInfo;
use Common\Models\Messages;

class ShopinfoController extends ControllerAdBase
{
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 店铺审核列表
     */
    public function getlistAction(){
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page']=1;
        }
        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $this->params['page_size']=10;
        }
        if(!isset($this->params['keywords']) || empty($this->params['keywords'])){
            $this->params['keywords']='';
        }

        $BShopUserDb = new BShopUsers();
        $get_list = $BShopUserDb->get_shop_user_info($this->params);
        $this->ok($get_list);
    }

    /**
     * 店铺基本信息
     */
    public function getshopinfoAction(){
        if(!isset($this->params['shop_id']) || empty($this->params['shop_id'])){
            $this->fail(-1,'shop_id?');
        }
        $BShopUserDb = new BShopUsers();
        $get_list = $BShopUserDb->get_shop_user_info($this->params);
        if(!$get_list['items']){
            $this->fail(-1,'没有找到记录行');
        }
        $this->ok($get_list['items'][0]);
    }


    public function shopcheckAction(){
        if(!isset($this->params['shop_id']) || empty($this->params['shop_id'])){
            $this->params['shop_id']=5;
        }
        if(!isset($this->params['is_check']) || empty($this->params['is_check'])){
            $this->params['is_check']=-1;
        }

        if($this->params['is_check'] == -1){
            if(!isset($this->params['check_tips']) || empty($this->params['check_tips'])){
                $this->fail(-1,'请填写审核不通过的原因');
            }
        }

        $this->dbMaster->begin();
        $BShopUserDb = new BShopUsers();
        $bshopuser = $BShopUserDb->findFirstById($this->params['shop_id']);
        if(!$bshopuser){
            $this->fail(-1,'没有找到信息');
        }
        if($bshopuser->status != 3){
            $this->fail(-1,'当前不能审核操作');
        }
        $bshopuser->status = $this->params['is_check'];
        $bshopuser->status_tips = isset($this->params['check_tips']) ? $this->params['check_tips'] : '';
        $bshopuser->check_time = date('Y-m-d H:i:s');
        if(!$bshopuser->save()){
            $this->dbMaster->rollback();
            $this->fail(-1,'审核失败');
        }

        $BShopUserInfoDb = new BshopUsersInfo();
        $bshopuserinfo = $BShopUserInfoDb->findFirstByShopId($bshopuser->id);
        if(!$bshopuserinfo){
            $bshopuserinfo = new BshopUsersInfo();
            $bshopuserinfo->shop_id = $bshopuser->id;
        }
        $bshopuserinfo->is_disable = $this->params['is_check'];
        if(!$bshopuserinfo->save()){
            $this->dbMaster->rollback();
            $this->fail(-1,'审核失败');
        }

        $FMessagesDb = new Messages();
        $FMessagesDb->user_id = $bshopuser->user_id;
        $FMessagesDb->title = "系统提醒";
        $FMessagesDb->content = $this->params['is_check'] == 1 ? "您提交的店铺申请审核通过了" : "您提交的店铺申请审核没有通过，不通过的原因【{$this->params['check_tips']}】";
        $FMessagesDb->created_at = date('Y-m-d H:i:s');
        $FMessagesDb->updated_at = date('Y-m-d H:i:s');
        $FMessagesDb->is_read = 0;
        if(!$FMessagesDb->save()){
            $this->dbMaster->rollback();
            $this->fail(-1,'审核失败');
        }

        $this->dbMaster->commit();
        $this->ok([]);
    }


    /**
     * 关闭
     */
    public function shopdisableAction(){
        if(!isset($this->params['shop_id']) || empty($this->params['shop_id'])){
            $this->params['shop_id']=5;
        }
        if(!isset($this->params['is_disable']) || empty($this->params['is_disable'])){
            $this->params['is_disable']=-1;
        }

        if($this->params['is_disable'] == -1){
            if(!isset($this->params['disable_tips']) || empty($this->params['disable_tips'])){
                $this->fail(-1,'请填写冻结的原因');
            }
        }

        $this->dbMaster->begin();
        $BShopUserDb = new BShopUsers();
        $bshopuser = $BShopUserDb->findFirstById($this->params['shop_id']);
        if(!$bshopuser){
            $this->fail(-1,'没有找到信息');
        }
        if(!in_array($this->params['is_disable'],[1,2])){
            $this->fail(-1,'当前不能操作');
        }
        $bshopuser->status = $this->params['is_check'];
        $bshopuser->check_time = date('Y-m-d H:i:s');
        if(!$bshopuser->save()){
            $this->dbMaster->rollback();
            $this->fail(-1,'失败');
        }

        $BShopUserInfoDb = new BshopUsersInfo();
        $bshopuserinfo = $BShopUserInfoDb->findFirstByShopId($bshopuser->id);
        if(!$bshopuserinfo){
            $bshopuserinfo = new BshopUsersInfo();
            $bshopuserinfo->shop_id = $bshopuser->id;
        }
        $bshopuserinfo->is_disable = $this->params['is_check'];
        $bshopuserinfo->disable_tips = isset($this->params['disable_tips']) ? $this->params['disable_tips'] : '';
        if(!$bshopuserinfo->save()){
            $this->dbMaster->rollback();
            $this->fail(-1,'失败');
        }

        $FMessagesDb = new Messages();
        $FMessagesDb->user_id = $bshopuser->user_id;
        $FMessagesDb->title = "系统提醒";
        $FMessagesDb->content = $this->params['is_check'] == 2 ? "您的店铺已经被平台冻结，原因：【{$this->params['disable_tips']}】" : "您的店铺已解冻";
        $FMessagesDb->created_at = date('Y-m-d H:i:s');
        $FMessagesDb->updated_at = date('Y-m-d H:i:s');
        $FMessagesDb->is_read = 0;
        if(!$FMessagesDb->save()){
            $this->dbMaster->rollback();
            $this->fail(-1,'审核失败');
        }

        $this->dbMaster->commit();
        $this->ok([]);
    }



    /**
     *
     */
    public function shop_goods_listAction(){
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['ppage']=1;
        }
        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $this->params['page_size']=10;
        }
        if(!isset($this->params['keywords']) || empty($this->params['keywords'])){
            $this->params['keywords']='';
        }
        $BShopGoodsDb = new BShopGoods();
        $bshopgppdslist = $BShopGoodsDb->get_user_shop($this->params);
        $this->ok($bshopgppdslist);
    }




    /**
     *
     */
    public function shop_goods_detailAction(){

    }


    public function shop_goods_checkAction(){
        if(!isset($this->params['goods_id']) || empty($this->params['goods_id'])){
            $this->params['goods_id']=1;
        }

        if(!isset($this->params['admin_status']) || empty($this->params['admin_status'])){
            $this->params['admin_status']=0;
        }
        if($this->params['admin_status'] == 0){
            if(!isset($this->params['admin_status_tips']) || empty($this->params['admin_status_tips'])){
                $this->fail(-1,'请输入下架原因');
            }
        }

        $BShopGoodsDb = new BShopGoods();
        $bshopgoods = $BShopGoodsDb->findFirstById($this->params['goods_id']);
        if(!$bshopgoods){
            $this->fail(-1,'当前商品不存在');
        }
        $bshopgoods->admin_status = $this->params['admin_status'];
        if(!$bshopgoods->save()){
            $this->dbMaster->rollback();
            $this->fail(-1,'失败');
        }

        $BShopUsersDb = new BShopUsers();
        $bshopusers = $BShopUsersDb->findFirstById($bshopgoods->shop_id);

        $FMessageDb = new Messages();
        $FMessageDb->user_id =$bshopusers->user_id;
        $FMessageDb->title = "商品下架通知";
        $FMessageDb->content = $this->params['admin_status'] == 1 ? "您的商品【{$bshopgoods->name}】已被管理员上架" : "您的商品【{$bshopgoods->name}】已被管理员下架，原因：{$this->params['admin_status_tips']}";
        $FMessageDb->created_at=date('Y-m-d H:i:s');
        $FMessageDb->updated_at=date('Y-m-d H:i:s');
        if(!$FMessageDb->save()){
            $this->dbMaster->rollback();
            $this->fail(-1,'失败');
        }
        $this->dbMaster->commit();
        $this->ok([]);
    }

    /**
     * 店铺权限
     */
    public function shopRuleListAction(){
        $BShopRuleDb = new BShopRule();
        $get_list = $BShopRuleDb->shopRuleList();
        $this->ok($get_list);
    }

    /**
     * 店铺权限组
     */
    public function shopRuleGroupListAction(){
        $BShopRoleDb = new BShopRoles();
        $get_list = $BShopRoleDb->ruleGroupList();
        $this->ok($get_list);
    }


    /**
     * 店铺的权限组
     */
    public function shopRuleToGroupListAction(){
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page']=1;
        }
        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $this->params['page_size']=10;
        }
        $BShopUsersDb = new BShopUsers();
        $get_list = $BShopUsersDb->shopToRole($this->params);
        $this->ok($get_list);
    }

}
